<?php

namespace App\Listeners;

use App\Events\CustomerHistoryCreated;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jenssegers\Agent\Agent;


class UpdateCustomerScoreInfo
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    private $agent;

    public function __construct()
    {
        $this->agent = new Agent();
    }

    /**
     * Handle the event.
     *
     * @param  CustomerHistoryCreated  $event
     * @return void
     */
    public function handle(CustomerHistoryCreated $event)
    {
        $cusomerHistorty= $event->getCustomerHistory();
        $cusomerHistorty->score = $cusomerHistorty->calculateScore();
        $cusomerHistorty->score_rank_id = $cusomerHistorty->getScoreRankId();
        $totalPayment = $cusomerHistorty->getTotalLoanAmount();
        $cusomerHistorty->min_total_payment = $totalPayment['min'];
        $cusomerHistorty->max_total_payment = $totalPayment['max'];
        $cusomerHistorty->nikkei_score = $cusomerHistorty->getNikkeiScore();
        $cusomerHistorty->random_score = $cusomerHistorty->getRandomScore();
        $cusomerHistorty->min_interests = $cusomerHistorty->score_rank->min_interests;
        $cusomerHistorty->max_interests = $cusomerHistorty->score_rank->max_interests;
        $cusomerHistorty->ip = request()->ip();
        $cusomerHistorty->operation_system = $this->agent->platform();
        $cusomerHistorty->user_agent = $this->agent->browser();
        $cusomerHistorty->save();

    }   
}
